//
//  SWEffectCollectionCell.swift
//  ChildStory
//
//  Created by linhan on 16/8/29.
//  Copyright © 2016年 Aiya. All rights reserved.
//

import Foundation
import UIKit

class SWEffectCollectionCell: UICollectionViewCell
{
    weak var thumb:UIImage?
    
    private var _change:Bool = true
    private var _nameLabel:UILabel = UILabel()
    private var _imageView:UIImageView = UIImageView()
    private var _contentView:UIView = UIView()
    
    
    var filter:SWBaseFilter?{
        didSet {
            layoutSubviews()
        }
    }
    
    override init(frame: CGRect)
    {
        super.init(frame: frame)
        setup()
    }
    
    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setup()
    {
        selectedBackgroundView = UIView()
        selectedBackgroundView?.backgroundColor = UIColor(hex: 0xf4c3c3)
        
        let contentInsets = UIEdgeInsetsMake(2, 2, 2, 2)
        _contentView.backgroundColor = UIColor(white: 0.97, alpha: 1)
        _contentView.frame = CGRectMake(contentInsets.left, contentInsets.top, width - contentInsets.left - contentInsets.right, height - contentInsets.top - contentInsets.bottom)
        contentView.addSubview(_contentView)
        
        _imageView.frame = CGRectMake(0, 0, _contentView.width, _contentView.width)
        _imageView.contentMode = .scaleAspectFill
        _imageView.clipsToBounds = true
        _contentView.addSubview(_imageView)
        
        let nameLabelY:CGFloat = _imageView.bottom
        _nameLabel.textColor = UIColor(hex: 0xb0b0b0)
        _nameLabel.font = UIFont.systemFont(ofSize:12)
        _nameLabel.textAlignment = .center
        _nameLabel.frame = CGRectMake(0, nameLabelY, _contentView.width, height - nameLabelY)
        _contentView.addSubview(_nameLabel)
    }
    
    override func prepareForReuse()
    {
        super.prepareForReuse()
        _change = true
    }
    
    override func layoutSubviews()
    {
        super.layoutSubviews()
        if let filter = filter,_change
        {
            _change = false
            _nameLabel.text = "\(filter.name)"
            
            if let thumb = thumb
            {
                if let cachedThumb = filter.thumb
                {
                    _imageView.image = cachedThumb
                }
                else
                {
                    filter.execute(image: thumb, info: nil, successCallback: {[weak self] image in
                        filter.thumb = image
                        DispatchQueue.main.async{
                            self?._imageView.image = image
                        }
                        
                        }, failCallback: {[weak self] error in
                            DispatchQueue.main.async{
                                self?._imageView.image = thumb
                            }
                    })//end execute
                }//end else
            }//end thumb
            
        }//end filter
    }
    
    
    
}
